package dao.Imp;

import dao.DaoLogic;
import javabean.MiniFund;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import tools.Dbutils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static tools.Dbutils.getSqlSession;
import static tools.Dbutils.logs;

public class DaoLogicImp {
    public static void main(String[] args) {
        /*logs();
        SqlSession sqlSession = getSqlSession();
        DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
        Double balance = mapper.findBalance();
        System.out.println("balance = " + balance);*/
    }

@Test
    public void findAllDao() {
        logs();
        SqlSession sqlSession = getSqlSession();
        DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
//        List<MiniFund> allDao = mapper.findAllDao();
    List<MiniFund> allDao = mapper.findAllDao();
    System.out.println("编号" + "\t" + "项目" + "\t" + "金额" + "\t" + "日期" + "\t" + "余额" + "\t");
        for (int i = 0; i < allDao.size(); i++) {
            System.out.println(allDao.get(i).getId() + "\t" + allDao.get(i).getProject() + "\t"
                    + allDao.get(i).getAmount() + "\t" + allDao.get(i).getDate()
                    + "\t" + allDao.get(i).getBalance());
        }
    }


    public void addAmountDao(String project, Double amount, String date) {
        logs();
        SqlSession sqlSession = getSqlSession();
        DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("project",project);
        objectObjectHashMap.put("amount",amount);
        objectObjectHashMap.put("date",date);
        Double balance = mapper.findBalance();
        balance+=amount;
        objectObjectHashMap.put("balance",balance);
        Integer update = mapper.addAmountDao(objectObjectHashMap);
        sqlSession.commit();
        System.out.println("---"+update+"笔收款已到账---");
    }


    public void consumeAmountDao(String project, Double amount, String date) {
        logs();
        SqlSession sqlSession = getSqlSession();
        DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("project",project);
        objectObjectHashMap.put("amount",-amount);
        objectObjectHashMap.put("date",date);
        Double balance = mapper.findBalance();
        System.out.println(balance);
        balance=balance-amount;
        objectObjectHashMap.put("balance",balance);
        Integer update = mapper.consumeAmountDao(objectObjectHashMap);
        sqlSession.commit();
        System.out.println("---您有"+update+"笔支出---");
    }
}
